#coding=utf-8
from django.db import models
from common.import_common import *
from report.forms import ReportForm

@login_required                         
def report(request, ctype, object_pk, template_name="report/report.html"):
    try:
        model_name = ctype.split(".", 1)
        model = models.get_model(*model_name)
        target = model._default_manager.get(pk=object_pk)
    except TypeError:
        raise Http404("can't find object")       

    if request.method == "POST":
        form = ReportForm(request, data=request.POST)
        if form.is_valid():
            form.save(request.user, target)
            return HttpResponseRedirect("/")

    form = ReportForm()
    return render_to_response(template_name, 
                            {"ctype": ctype,
                            "object_pk":object_pk,
                            "form":form
                            },
                              context_instance=RequestContext(request))